package com.dm.service.impl;

import com.dm.client.dm_item.RestDmCinemaClient;
import com.dm.client.dm_base.RestDmImageClient;
import com.dm.client.dm_item.RestDmItemClient;
import com.dm.client.dm_item.RestDmItemCommentClient;
import com.dm.client.dm_scheduler.RestDmSchedulerClient;
import com.dm.client.dm_scheduler.RestDmSchedulerSeatClient;
import com.dm.client.dm_scheduler.RestDmSchedulerSeatPriceClient;
import com.dm.common.*;
import com.dm.dto.Dto;
import com.dm.dto.DtoUtil;
import com.dm.pojo.dm_item.DmCinema;
import com.dm.pojo.dm_base.DmImage;
import com.dm.pojo.dm_item.DmItem;
import com.dm.pojo.dm_item.DmItemComment;
import com.dm.pojo.dm_scheduler.DmScheduler;
import com.dm.pojo.dm_scheduler.DmSchedulerSeat;
import com.dm.pojo.dm_scheduler.DmSchedulerSeatPrice;
import com.dm.service.ItemDetailService;
import com.dm.vo.HotItemVo;
import com.dm.vo.ItemCommentVo;
import com.dm.vo.ItemDetailVo;
import com.dm.vo.ItemSchedulerVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ItemDetailServiceImpl implements ItemDetailService {


    @Resource
    private RestDmImageClient dmImageClient;
    @Resource
    private RestDmCinemaClient restDmCinemaClient;
    @Resource
    private RestDmItemClient restDmItemClient;
    @Resource
    private RestDmSchedulerClient restDmSchedulerClient;
    @Resource
    private RestDmSchedulerSeatPriceClient restDmSchedulerSeatPriceClient;
    @Resource
    private RestDmSchedulerSeatClient restDmSchedulerSeatClient;
    @Resource
    private RestDmItemCommentClient restDmItemCommentClient;
    @Resource
    private RestDmImageClient restDmImageClient;


    @Override
    public Dto<ItemDetailVo> queryItemDetail(Long id) throws Exception {
        DmItem dmItem = restDmItemClient.getDmItemById(id);
        if (EmptyUtils.isEmpty(dmItem)) {
            return null;
        }
        //查询图片
        List<DmImage> dmImageList = getImageList(dmItem.getId(), Constants.Image.ImageType.carousel, Constants.Image.ImageCategory.item);
        //查询剧院
        DmCinema dmCinema = restDmCinemaClient.getDmCinemaById(dmItem.getCinemaId());
        //组装返回数据
        ItemDetailVo itemDetailVo = copyData(dmItem, dmCinema, dmImageList);
        return DtoUtil.returnDataSuccess(itemDetailVo);
    }


    @Override
    public List<ItemSchedulerVo> getItemSchedulerById(String id) throws Exception {
        Map<String,Object> map=new HashMap<>();
        map.put("itemId",id);
        List<DmScheduler> list=restDmSchedulerClient.getDmSchedulerListByMap(map);
        List<ItemSchedulerVo> list2=new ArrayList<>();
        for(int i=0;i<list.size();i++) {
            DmScheduler dmScheduler=list.get(i);
            ItemSchedulerVo itemSchedulerVo=new ItemSchedulerVo();
            BeanUtils.copyProperties(dmScheduler,itemSchedulerVo);
            itemSchedulerVo.setStartTime(DateUtil.format(dmScheduler.getStartTime()));
            itemSchedulerVo.setEndTime(DateUtil.format(dmScheduler.getEndTime()));
            list2.add(itemSchedulerVo);
        }
        return list2;

    }

    @Override
    public List<DmSchedulerSeatPrice> queryItemPrice(String id) throws Exception {
        Map<String,Object> map=new HashMap<>();
        map.put("scheduleId",id);
        //查询排期价格
        List<DmSchedulerSeatPrice> dmSchedulerSeatPrice= restDmSchedulerSeatPriceClient.getDmSchedulerSeatPriceListByMap(map);
            if(EmptyUtils.isNotEmpty(dmSchedulerSeatPrice)) {
                //查询排期是否有座位

                for(int i=0;i<dmSchedulerSeatPrice.size();i++){
                    map.put("areaLevel",dmSchedulerSeatPrice.get(i).getAreaLevel());
                    map.put("status",1);
                    Integer stock = restDmSchedulerSeatClient.getDmSchedulerSeatCountByMap(map);
                    if(EmptyUtils.isNotEmpty(stock)||stock>0){
                        dmSchedulerSeatPrice.get(i).setIsHaveSeat(1);
                    }else{
                        dmSchedulerSeatPrice.get(i).setIsHaveSeat(0);
                    }
                }
            }else{
                return null;
            }
        return dmSchedulerSeatPrice;
    }

    @Override
    public List<ItemCommentVo> queryItemComment(String id) throws Exception {
        Map<String,Object> map=new HashMap<>();
        map.put("itemId",id);
        //获取评论集合
        List<DmItemComment> list=restDmItemCommentClient.getDmItemCommentListByMap(map);
        List<ItemCommentVo> listVo=new ArrayList<>();
        if(EmptyUtils.isNotEmpty(list)){
            for(int i=0;i<list.size();i++){
                DmItemComment dmItemComment=list.get(i);
                ItemCommentVo itemCommentVo=new ItemCommentVo();
                itemCommentVo.setCreatedTime(DateUtil.format(dmItemComment.getCreatedTime()));
                //复制
                BeanUtils.copyProperties(dmItemComment,itemCommentVo);
                //获取用户图片
                map=new HashMap<>();
                map.put("targetId",itemCommentVo.getUserId());
                map.put("category",Constants.Image.ImageCategory.user);
                DmImage dmImage=null;
                List<DmImage> dmImageList=restDmImageClient.getDmImageListByMap(map);
                if(EmptyUtils.isNotEmpty(dmImageList)){
                    dmImage=dmImageList.get(0);
                    itemCommentVo.setImgUrl(dmImage.getImgUrl());
                }
                listVo.add(itemCommentVo);
            }

        }else {
            return null;
        }
        return listVo;
    }

    private ItemDetailVo copyData(DmItem dmItem, DmCinema dmCinema, List<DmImage> dmImageList) throws ParseException {
        ItemDetailVo itemDetailVo = new ItemDetailVo();
        BeanUtils.copyProperties(dmItem, itemDetailVo);
        itemDetailVo.setStartTime(DateUtil.format(dmItem.getStartTime()));
        itemDetailVo.setEndTime(DateUtil.format(dmItem.getEndTime()));
        itemDetailVo.setState(dmItem.getState() + "");
        itemDetailVo.setImgUrl(dmImageList.get(0).getImgUrl());
        if (EmptyUtils.isEmpty(dmCinema)) {
            //封装剧院信息
            BeanUtils.copyProperties(dmCinema, itemDetailVo);
        }
        itemDetailVo.setId(dmItem.getId());
        return itemDetailVo;
    }

    /**
     * 查询图片
     *
     * @param id
     * @param type
     * @param category
     * @return
     */
    public List<DmImage> getImageList(Long id, Integer type, Integer category) throws Exception {
        Map<String, Object> paramImage = new HashMap<String, Object>();
        paramImage.put("targetId", id);
        paramImage.put("type", type);
        paramImage.put("category", category);
        return dmImageClient.getDmImageListByMap(paramImage);
    }




    @Override
    //分页传参单引号多加问题
    public List<HotItemVo> queryBanner() throws Exception {
        //查询轮播图前5个
        Map<String, Object> param = new HashMap<>();
        param.put("isBanner", 1);
       /* param.put("beginPos",0);
        param.put("pageSize",5);*/
        List<DmItem> dmItemList = restDmItemClient.getDmItemListByMap(param);
        dmItemList=dmItemList.subList(0,5);
        //组装接口返回数据
        List<HotItemVo> hotItemVoList = new ArrayList<HotItemVo>();
        if (EmptyUtils.isEmpty(dmItemList)) {
            return null;
        }
        for (DmItem dmItem : dmItemList) {
            HotItemVo hotItemVo = new HotItemVo();
            BeanUtils.copyProperties(dmItem, hotItemVo);
            //查询图片信息
            List<DmImage> dmImageList = dmImageClient.queryDmImageList(dmItem.getId(),
                    Constants.Image.ImageType.carousel,
                    Constants.Image.ImageCategory.item);
            //组装图片信息
            hotItemVo.setImgUrl(EmptyUtils.isEmpty(dmImageList) ? null : dmImageList.get(0).getImgUrl());
            hotItemVoList.add(hotItemVo);
        }
        return hotItemVoList;
    }
}
